package com.stlm2.controller.user;

import com.stlm2.core.base.BaseController;
import com.stlm2.dems.user.entity.IsInit;
import com.stlm2.dems.user.service.RoleService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by zlq on 2017/8/1.
 */
@Controller
@RequestMapping("/roleC")
public class RoleController  extends BaseController {
    @Resource
    private RoleService roleService;
    /*zlq
*查找后台角色列表页面
* */
    @ResponseBody
    @RequestMapping(value="/list",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){

        modelAndView.setViewName("/role/list");
        return modelAndView;
    }

    	/*
	*查询有效的可初始化的角色
	 */
        @ResponseBody
        @RequestMapping(value="/getInitValidRole.json",produces="application/json;charset=UTF-8")
        public  String getInitValidRole(ModelMap map){
            ModelMap data=null;
            try {
                data=roleService.getInitValidRole(map, IsInit.Allow);
            }catch (Exception e){
                e.printStackTrace();
                return CodeContent.returnFailure("查询失败");
            }
            return  new JSONObject(data).toString();

        }

    /*
*查询有效的不可始化的角色
*/
    @ResponseBody
    @RequestMapping(value="/getNotAllowRole.json",produces="application/json;charset=UTF-8")
    public  String getNotAllowRole(ModelMap map){
        ModelMap data=null;
        try {
            data=roleService.getInitValidRole(map, IsInit.NotAllow);
        }catch (Exception e){
            e.printStackTrace();
            return CodeContent.returnFailure("查询失败");
        }
        return  new JSONObject(data).toString();

    }

    /*zlq
    *添加角色
    * @param  token
    * @param  name :角色名字
    * @param  label :标签名称
    * @param  isInit :是否允许创建用户时直接使用
     */
    @ResponseBody
    @RequestMapping(value="/addRole.json",produces="application/json;charset=UTF-8")
    public  String addRole(ModelMap model,String token ,String name,String label,String isInit){
        String data=null;
        try {
            data=roleService.addRole( model,token,name,label,isInit);
        }catch (Exception e){
            e.printStackTrace();
            return CodeContent.returnFailure("添加失败");
        }
        return  data;

    }
    /*
*查询有效的角色
* @param  token
* * @param  page ：页码
* @param  token
*/
    @ResponseBody
    @RequestMapping(value="/getNormalRole.json",produces="application/json;charset=UTF-8")
    public  String getNormalRole(ModelMap map,String token,String page){
        String data=null;
        try {
            data=roleService.getNormalRole(map,token,page);
        }catch (Exception e){
            e.printStackTrace();
            return CodeContent.returnFailure("添加失败");
        }
        return  data;

    }

}
